package com.jymf.login.controller;

import com.jymf.common.HttpJsonObject;
import com.jymf.common.MD5;
import com.jymf.common.SessionUtil;
import com.jymf.common.VerifyCodeUtils;
import com.jymf.login.service.LoginService;
import com.jymf.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * Created by z on 2017/5/22.
 */
@Controller
public class LoginController extends HttpJsonObject{

    @Autowired
    private LoginService loginService;
    /**
     * 登录验证 失败跳转index.jsp  成功main.jsp
     * @author zyj
     * @date 2017/05/22
     * @param 
     * @return 
     **/
    @RequestMapping(value="/login", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView login(RedirectAttributes attributes, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String lgName = request.getParameter("names");
        String lgpasswd = request.getParameter("passwds");
        String verCode = request.getParameter("verCode");
        ModelAndView modelAndView = new ModelAndView();

        Users userSession = SessionUtil.getSession(request);

        //step1:判断session是否存在
        String name = "";
        String passwd = "";
        if(userSession!=null&&userSession.getLgname()!=null){//获取用户名和密码
            name = userSession.getLgname();
            passwd = userSession.getLgpasswd();
            modelAndView.setViewName("/main");
            modelAndView.addObject("user", name);
            return modelAndView;
        }else if(userSession!=null&&lgpasswd!=null&&!"".equals(lgpasswd)) {
            name = request.getParameter("names");
            passwd = MD5.EncoderPwdByMd5(lgpasswd);
        }else{//没有session和登录密码直接跳转登录页
            modelAndView.setViewName("/index");
            return modelAndView;
        }

        //step2: 判断验证码
        String verCodes = userSession.getVerCode();
        if(userSession.getUsrname()==null&&(verCode==null||verCode==""||verCodes==null||verCodes=="")){
            modelAndView.setViewName("/index");
            modelAndView.addObject("value", false);
            modelAndView.addObject("info", "验证码为空");
            return modelAndView;
        }else if(verCode!=null&&verCode!=""&&verCodes!=null&&verCodes!=""&&!verCodes.equals(verCode.toLowerCase())) {
            modelAndView.setViewName("/index");
            modelAndView.addObject("value", false);
            modelAndView.addObject("info", "验证码错误");
            return modelAndView;
        }

        //step3:判断用户名和密码
        Users user = loginService.getUser(name, passwd, request);
        if(user!=null){
            modelAndView.setViewName("/main");
            modelAndView.addObject("user", user.getUsrname());
        }else{
            modelAndView.setViewName("/index");
            modelAndView.addObject("value", false);
            modelAndView.addObject("info", "用户名或密码错误");
        }

        return modelAndView;
    }

    /**
     * 注销登录
     * @param response
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    @RequestMapping(value="/register", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView register( HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        request.getSession().removeAttribute("userInfo");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/index");

        return modelAndView;
    }

    /**
     * 验证码
     * @param response
     * @param request
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    @RequestMapping(value="/authimage", method = { RequestMethod.POST, RequestMethod.GET })
    public void authimage(HttpServletResponse response, HttpServletRequest request) throws Exception {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //存入会话session
        HttpSession session = request.getSession(true);
        Object userInfo = session.getAttribute("userInfo");
        Users user = null;
        if(userInfo!=null) {
            user = (Users) userInfo;
        } else {
            user = new Users();
        }

        //删除以前的
        user.setVerCode("");
        user.setVerCode(verifyCode.toLowerCase());
        //session.removeAttribute("verCode");
        //session.setAttribute("verCode", verifyCode.toLowerCase());
        SessionUtil.setSession(request, user);//放入session
        //生成图片
        int w = 100, h = 30;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }


}
